﻿using Sustainalytics.Claims;
using Sustainalytics.ScreeningTool.BL;
using Sustainalytics.ScreeningTool.Service.Utility;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Sustainalytics.ScreeningTool.Service
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/criterialevel1/{criteriaLevel1Id}/criterialevel2")]
    public class CriteriaLevel2Controller : ApiController, ICriteriaLevel2Controller
    {
        private readonly ICompanyScreening _companyScreening;
        private readonly IClaimsChecker _claimsChecker;

        public CriteriaLevel2Controller(ICompanyScreening companyScreening, IClaimsChecker claimsChecker)
        {
            _companyScreening = companyScreening;
            _claimsChecker = claimsChecker;
        }

        //[CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]

        [Route("")]
        public HttpResponseMessage Get(Guid criteriaLevel1Id, [FromUri]ListOptions options, bool includeAllFields = false, string username = "")
        {
            try
            {
                var criteriaLevel2List = _companyScreening.ListCriteriaLevelTwo(criteriaLevel1Id);
                if (includeAllFields)
                {
                    var baseCriteria = _companyScreening.ListBaseCriteria(null);

                    List<CriteriaLevel2Dto> criteriaLevel2Dtos = new List<CriteriaLevel2Dto>();
                    criteriaLevel2List.ForEach(cr2 =>
                    {
                        criteriaLevel2Dtos.Add(CriteriaLevel2Dto.CreateComplete(cr2, baseCriteria.Where(bc => bc.CriteriaLevel2Id == cr2.Id).ToList(), false, this, _claimsChecker));
                    });

                    return Request.CreateResponse(HttpStatusCode.OK, criteriaLevel2Dtos);
                }

                return Request.CreateResponse(HttpStatusCode.OK, criteriaLevel2List.ConvertAll<object>(cr2 => CriteriaLevel2Dto.CreateWithNameId(cr2)));
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new { Code = ErrorCodes.ScreeningTool_LoadingCriteriaTabs });
            }
        }

        //[CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("{id}")]
        public HttpResponseMessage Get(Guid id, bool includeAllFields = false, string username = "")
        {
            try
            {
                var criteriaLevel2 = _companyScreening.GetCriteriaLevelTwo(id);
                if (includeAllFields)
                {
                    var baseCriteria = _companyScreening.ListBaseCriteria(null);

                    return Request.CreateResponse(HttpStatusCode.OK, CriteriaLevel2Dto.CreateComplete(criteriaLevel2, baseCriteria.Where(bc => bc.CriteriaLevel2Id == criteriaLevel2.Id).ToList(), false, this, _claimsChecker));
                }

                return Request.CreateResponse(HttpStatusCode.OK, CriteriaLevel2Dto.CreateWithNameId(criteriaLevel2));
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new { Code = ErrorCodes.ScreeningTool_LoadingCriteriaTabs });
            }
        }
    }
}
